package com.weijian.controller;

import com.weijian.common.Result;
import com.weijian.entity.VideoInfo;
import com.weijian.service.VideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class UploadController {

    @Autowired
    private VideoInfoService videoInfoService;

    /**
     * 简单上传视频
     */
    @PostMapping("/videos/upload")
    public Result<?> uploadVideo(@RequestParam("file") MultipartFile file) {
        try {
            log.info("开始上传视频文件: {}", file.getOriginalFilename());
            
            if (file.isEmpty()) {
                return Result.error("上传文件不能为空");
            }
            
            VideoInfo videoInfo = videoInfoService.uploadVideo(file);
            return Result.success("视频上传成功", videoInfo);
            
        } catch (IllegalArgumentException e) {
            log.warn("文件验证失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("视频上传失败: {}", e.getMessage(), e);
            return Result.error("视频上传失败: " + e.getMessage());
        }
    }
}